#!/bin/bash
# derpm.sh
#
# Unpack an 'rpm' archive

E_NOT_RPM=65 # Not a RPM file
E_FILE=66    #Bad filename

if [ -z $1 ]; then
	echo "Usage: $0 <TARGET_FILE>"
	exit 1
fi

if [ ! -f $1 ]; then
	echo "File $1 does not exit"
	exit $E_FILE
fi

RPM=rpm

if [ ! file -i $1 | fgrep $RPM ]; then
	echo "File $1 is not a RPM archive!"
	exit $E_NOT_RPM
fi

# TEMPFILE with unique name, since $$ is the PID of the script
TEMPFILE=$$.cpio

# Convert rmp archive into cpio one
rpm2cpio < $1 > $TEMPFILE
               
# Unpacking cpio file
cpio --make-directories -F $TEMPFILE -i

# Deleting cpio archive
rm -f $TEMPFILE

exit 0
